package ${packageName}.${moduleName}.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.ko.core.web.BaseController;
import org.ko.core.websupport.Reply;
import org.ko.core.websupport.log.Log;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import ${packageName}.${moduleName}.controller.model${subModuleName}.${ClassName}Model;
import ${packageName}.${moduleName}.service${subModuleName}.${ClassName}Service;

/**
 * ${functionName}Controller
 * @author ${classAuthor}
 * @version ${classVersion}
 */
@Controller
@RequestMapping("/${ClassName?lower_case}")
public class ${ClassName}Controller extends BaseController{

	@Autowired
	private ${ClassName}Service service;
	
	@Override
	protected String getTemplatePath() {
		return "car/${ClassName?lower_case}/";
	}

	@RequestMapping(value="add", method = RequestMethod.GET)
	public String add(){
		return view(ADD);
	}
	
	/**
	 * 查看实体
	 * @param req
	 * @param model
	 * @param viewMap
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="view")
	public String view(HttpServletRequest req, ${ClassName}Model model, ModelMap viewMap)
		throws Exception {
		service.view(model);
		
		viewMap.addAttribute(ENTITY, model.getBean());
		return view(VIEW);
	}
	
	/**
	 * 判断指定的${classNameChinese!ClassName}是否存在
	 * @param req
	 * @param model
	 * @return
	 */
	@RequestMapping(value="exist")
	@ResponseBody
	public Reply exist(HttpServletRequest req, ${ClassName}Model model){
		Reply re = new Reply();
		try {
			service.isExist(model);
			re.setMessage(model.getId());
		} catch (Exception e) {
			re.error(e);
		}
		return re;
	}
	
	@Log(message="创建了${classNameChinese!ClassName}", category="${ClassName?upper_case}")
	@RequestMapping(value="create")
	public String create(HttpServletRequest request, HttpServletResponse response, ${ClassName}Model model){
		try {
			service.create(model);
			
			return redirect("list.do");
		} catch (Exception e) {
			System.err.println("出错啦！");
			e.printStackTrace();
			
			return redirect("list.do");
		}
	}
	
	@Log(message="Ajax创建了${classNameChinese!ClassName}", category="${ClassName?upper_case}")
	@RequestMapping(value="createAjax")
	@ResponseBody
	public Reply createAjax(HttpServletRequest req, ${ClassName}Model model)
			throws Exception {
		Reply re = new Reply();
		try {
			service.create(model);
			re.setMessage("创建成功");
		} catch (Exception e) {
			re.error(e);
			e.printStackTrace();
		}
		return re;
	}
	
	@Log(message="删除了${classNameChinese!ClassName}[id={0}]", params={"id"}, category="${ClassName?upper_case}")
	@RequestMapping(value="delete")
	@ResponseBody
	public Reply delete(HttpServletRequest req, ${ClassName}Model model)
			throws Exception {
		Reply re = new Reply();
		try {
			service.delete(model);
			re.setMessage("删除成功");
		} catch (Exception e) {
			re.error(e);
			e.printStackTrace();
		}
		return re;
	}
	
	
	@RequestMapping(value="list")
	public String list(HttpServletRequest req, ${ClassName}Model model, ModelMap map) throws Exception{
		service.list(model);
		map.addAttribute(PAGINATION, model);
		
		return view(LIST);
	}
}